package live.zs.securitystarter.config;

import live.zs.securitystarter.entity.MyUsersEntity;
import live.zs.securitystarter.repo.MyUserRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author madison
 * @description
 * @date 2021/7/16 11:39
 */
public class CustomUserDetailsService implements UserDetailsService {
    private Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);

    @Autowired
    private MyUserRepo userRepo;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("CustomUserDetailsService:{}", username);
        MyUsersEntity usersEntity = userRepo.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException(username));

        return User.builder()
                .username(usersEntity.getUsername())
                .password(usersEntity.getPassword())
//                .roles("MYROLES")
                .authorities(AuthorityUtils.createAuthorityList("myauthorities"))
                .build();
//        return new CustomUser(username, "6854e69483f77563dc6d65b27caea9ba0ca063e9ca3add5b051f8c7e9517d6be29eb5c2308e749f9", AuthorityUtils
//                .createAuthorityList());
    }
}
